package com.zyh.aop;

import com.zyh.entity.NACheck;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Scanner;

@Component
@Aspect
public class WebControllerAop {
    @Pointcut("execution(* com.zyh.controller.HomeController.doSave(..))")

    public void executeService() {
    }

    @Before("executeService()")
    public void doBefore(JoinPoint joinPoint){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        NACheck naCheck=new NACheck();
        System.out.println("健康码-key a y/n");
        Scanner scanner = new Scanner(System.in);
        String s = scanner.next();
        System.out.println("AOP拦截成功");
        if (!"Y".equals(s) && !"y".equals(s)){
            session.getAttribute("student");
            try {
                session.setAttribute("pass","false");
                response.sendRedirect("/err");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
                session.setAttribute("pass","true");
        }
    }
}

